package org.arch_learn.servlet_container.components.connector.adapters;

import io.netty.handler.codec.http.DefaultHttpRequest;
import io.netty.handler.codec.http.HttpRequest;
import org.arch_learn.servlet_container.pojo.CustomHttpServletRequest;

import java.util.stream.Collectors;

public class ServletRequestAdapter {
    public static CustomHttpServletRequest getHttpServletRequest(HttpRequest nettyHttpRequest) {

        String startLine = nettyHttpRequest.method().name() + " "
                + nettyHttpRequest.uri() + " "
                + nettyHttpRequest.protocolVersion().text();

        String headers = nettyHttpRequest.headers()
                .entries().stream()
                .map(v -> {
                    String key = v.getKey();
                    String value = v.getValue();
                    return key + ": " + value;
                }).collect(Collectors.joining("\n"));


        System.out.println("startLine为：\n" + startLine);
        System.out.println("header为：\n" + headers);
        System.out.println("body为：\n" + null);
        CustomHttpServletRequest customHttpServletRequest = new CustomHttpServletRequest(startLine, headers, null);
        return customHttpServletRequest;
    }
}
